<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="ReentrantLock一个类的源码分析，主要就是看 继承关系、成员变量、构造方法、主要方法 这四个 一、继承关系public class ReentrantLock implements Lock, java.io.Serializable只实现了两个接口：Lock 和 java.io.Serializable 二、成员变量private final Sync sync;只有一个同步器，但是定">
<meta property="og:type" content="article">
<meta property="og:title" content="JAVA多线程-ReentrantLock">
<meta property="og:url" content="http://yoursite.com/2020/05/29/JAVA%E5%A4%9A%E7%BA%BF%E7%A8%8B-ReentrantLock/index.html">
<meta property="og:site_name" content="小蜗学习之旅">
<meta property="og:description" content="ReentrantLock一个类的源码分析，主要就是看 继承关系、成员变量、构造方法、主要方法 这四个 一、继承关系public class ReentrantLock implements Lock, java.io.Serializable只实现了两个接口：Lock 和 java.io.Serializable 二、成员变量private final Sync sync;只有一个同步器，但是定">
<meta property="article:published_time" content="2020-05-29T14:34:14.000Z">
<meta property="article:modified_time" content="2020-05-31T06:46:21.929Z">
<meta property="article:author" content="小蜗">
<meta property="article:tag" content="JAVA基础">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/05/29/JAVA多线程-ReentrantLock/"/>





  <title>JAVA多线程-ReentrantLock | 小蜗学习之旅</title>
  








<meta name="generator" content="Hexo 4.2.1"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>	
	<a href="https://github.com/xiaoawo" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
	<a href="https://github.com/xiaoawo" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小蜗学习之旅</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/home" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/05/29/JAVA%E5%A4%9A%E7%BA%BF%E7%A8%8B-ReentrantLock/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="小蜗">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xiaoawo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小蜗学习之旅">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JAVA多线程-ReentrantLock</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-05-29T22:34:14+08:00">
                2020-05-29
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JAVA%E5%A4%9A%E7%BA%BF%E7%A8%8B/" itemprop="url" rel="index">
                    <span itemprop="name">-JAVA多线程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="ReentrantLock"><a href="#ReentrantLock" class="headerlink" title="ReentrantLock"></a>ReentrantLock</h2><p><strong>一个类的源码分析，主要就是看 继承关系、成员变量、构造方法、主要方法 这四个</strong></p>
<h3 id="一、继承关系"><a href="#一、继承关系" class="headerlink" title="一、继承关系"></a>一、继承关系</h3><pre><code>public class ReentrantLock implements Lock, java.io.Serializable</code></pre><p>只实现了两个接口：Lock 和 java.io.Serializable</p>
<h3 id="二、成员变量"><a href="#二、成员变量" class="headerlink" title="二、成员变量"></a>二、成员变量</h3><pre><code>private final Sync sync;</code></pre><p>只有一个同步器，但是定义了三个内部类：  </p>
<ul>
<li>abstract static class Sync extends AbstractQueuedSynchronizer</li>
<li>static final class NonfairSync extends Sync</li>
<li>static final class FairSync extends Sync</li>
</ul>
<p>可以看到，Sync只是一个继承了AQS的抽象同步器，真正的实现是：NonfairSync 和 FairSync</p>
<h4 id="1、Sync的具体实现"><a href="#1、Sync的具体实现" class="headerlink" title="1、Sync的具体实现"></a>1、Sync的具体实现</h4><pre><code>abstract static class Sync extends AbstractQueuedSynchronizer {
    private static final long serialVersionUID = -5179523762034025860L;//不用管

    abstract void lock();//具体实现有子类来做

    final boolean nonfairTryAcquire(int acquires) {//非公平的尝试获取锁
        final Thread current = Thread.currentThread();
        int c = getState();//AQS的方法，返回当前的state（当前被持有的次数）
        if (c == 0) {//state未被任何线程持有
            if (compareAndSetState(0, acquires)) {//AQS的方法，使用CAS获取锁
                setExclusiveOwnerThread(current);//Exclusive：独享的
                return true;
            }
        }else if (current == getExclusiveOwnerThread()) {//判断持有锁的线程是否为当前线程，是，则重入
            int nextc = c + acquires;
            if (nextc &lt; 0) // overflow
                throw new Error(&quot;Maximum lock count exceeded&quot;);
            setState(nextc);
            return true;
        }
        return false;
    }

    protected final boolean tryRelease(int releases) {//尝试释放锁
        int c = getState() - releases;
        if (Thread.currentThread() != getExclusiveOwnerThread())//当前线程没有持有锁时会抛出异常
            throw new IllegalMonitorStateException();
        boolean free = false;
        if (c == 0) {
            free = true;
            setExclusiveOwnerThread(null);//设置锁的持有者为null
        }
        setState(c);//重置state
        return free;
    }

    //判断当前是否是锁的持有者
    protected final boolean isHeldExclusively() {
        // While we must in general read state before owner,
        // we don&apos;t need to do so to check if current thread is owner
        return getExclusiveOwnerThread() == Thread.currentThread();
    }

    //新建Condition
    final ConditionObject newCondition() {
        return new ConditionObject();
    }

    //获取线程的持有者
    final Thread getOwner() {
        return getState() == 0 ? null : getExclusiveOwnerThread();
    }

    //获取锁被持有的次数（state）
    final int getHoldCount() {
        return isHeldExclusively() ? getState() : 0;
    }

    //判断是否上锁了
    final boolean isLocked() {
        return getState() != 0;
    }

    /**
     * Reconstitutes the instance from a stream (that is, deserializes it).
     */
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();
        setState(0); // reset to unlocked state
    }
}</code></pre><p>final boolean nonfairTryAcquire(int acquires) :</p>
<div id="flowchart-0" class="flow-chart"></div>

<p>protected final boolean tryRelease(int releases)</p>
<div id="flowchart-1" class="flow-chart"></div>


<p>所以，sync里就两个主要的方法：一个是非公平的尝试获取锁，另一个就是尝试进行一次锁的释放</p>
<p>再看非公平和公平的实现方式</p>
<h4 id="2、NonfairSync的具体实现"><a href="#2、NonfairSync的具体实现" class="headerlink" title="2、NonfairSync的具体实现"></a>2、NonfairSync的具体实现</h4><pre><code>static final class NonfairSync extends Sync {
    private static final long serialVersionUID = 7316153563782823691L;

    //实现了lock
    final void lock() {
        if (compareAndSetState(0, 1))//使用CAS尝试一次获取锁
            setExclusiveOwnerThread(Thread.currentThread());
        else
            acquire(1);//AQS的方法【获锁失败了，就添加到队列中】
    }

    protected final boolean tryAcquire(int acquires) {
        return nonfairTryAcquire(acquires);
    }
}</code></pre><h4 id="3、FairSync的具体实现"><a href="#3、FairSync的具体实现" class="headerlink" title="3、FairSync的具体实现"></a>3、FairSync的具体实现</h4><pre><code>static final class FairSync extends Sync {
    private static final long serialVersionUID = -3000897897090466540L;

    final void lock() {
        acquire(1);
    }

    /**
     * Fair version of tryAcquire.  Don&apos;t grant access unless
     * recursive call or no waiters or is first.
     */
    protected final boolean tryAcquire(int acquires) {
        final Thread current = Thread.currentThread();
        int c = getState();
        if (c == 0) {
            if (!hasQueuedPredecessors() &amp;&amp; compareAndSetState(0, acquires)) {//只比非公平的方式多了一个hasQueuedPredecessors()判断（是否为队列的头）
                setExclusiveOwnerThread(current);
                return true;
            }
        }else if (current == getExclusiveOwnerThread()) {
            int nextc = c + acquires;
            if (nextc &lt; 0)
                throw new Error(&quot;Maximum lock count exceeded&quot;);
            setState(nextc);
            return true;
        }
        return false;
    }
}</code></pre><p>protected final boolean tryAcquire(int acquires)</p>
<div id="flowchart-2" class="flow-chart"></div>

<h4 id="4、小结"><a href="#4、小结" class="headerlink" title="4、小结"></a>4、小结</h4><ul>
<li>锁释放，对公平和非公平而言貌似没有区别，所以在sync中实现了tryRelease,同时sync里也实现了非公平的获取锁的方法nonfairTryAcquire</li>
<li>对于公平锁和非公平锁而言，其实仅有两处代码上的不同<ul>
<li>lock()：非公平先进行了一次CAS加锁</li>
<li>tryAcquire()：公平锁需要进行一次是否为队列头节点的判断</li>
</ul>
</li>
</ul>
<h3 id="三、构造方法"><a href="#三、构造方法" class="headerlink" title="三、构造方法"></a>三、构造方法</h3><p>就两个，本质上就是返回NonfairSync或FairSync的实例，默认为NonfairSync</p>
<pre><code>public ReentrantLock() {
    sync = new NonfairSync();
}

public ReentrantLock(boolean fair) {
    sync = fair ? new FairSync() : new NonfairSync();
}</code></pre><h3 id="四、主要方法"><a href="#四、主要方法" class="headerlink" title="四、主要方法"></a>四、主要方法</h3><h4 id="1、lock"><a href="#1、lock" class="headerlink" title="1、lock"></a>1、lock</h4><pre><code>public void lock() {
    sync.lock();
}</code></pre><h4 id="2、unlock"><a href="#2、unlock" class="headerlink" title="2、unlock"></a>2、unlock</h4><pre><code>public void unlock() {//AQS的方法，但是tryRelease的具体实现，又由具体实例来做的，就是上面分析那些
    sync.release(1);
}&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.7/raphael.min.js&quot;&gt;&lt;/script&gt;&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/flowchart/1.6.5/flowchart.min.js&quot;&gt;&lt;/script&gt;{% raw %}<textarea id="flowchart-0-code" style="display: none">st=>start: Start
op1=>operation: 获取当前线程
op2=>operation: 获取state

cond2=>condition: 使用CAS让当前线程持有锁

cond3=>condition: 当前线程是否已经持有锁了

cond1=>condition: state==0 ?

op3=>operation: 重置state


e1=>end: return true
e2=>end: return false
e3=>end: return false
st->op1->op2->cond1
cond1(yes)->cond2
cond1(no)->cond3
cond2(yes)->e1
cond2(no)->e2
cond3(yes)->op3->e1
cond3(no)->e3</textarea><textarea id="flowchart-0-options" style="display: none">{"scale":1,"line-width":2,"line-length":50,"text-margin":10,"font-size":12}</textarea><script>  var code = document.getElementById("flowchart-0-code").value;  var options = JSON.parse(decodeURIComponent(document.getElementById("flowchart-0-options").value));  var diagram = flowchart.parse(code);  diagram.drawSVG("flowchart-0", options);</script>{% endraw %}{% raw %}<textarea id="flowchart-1-code" style="display: none">st=>start: Start
cond1=>condition: 判断当前线程是否持有锁

op0=>operation: boolean free = false
cond2=>condition: state==0?

op3=>operation: free = true
op1=>operation: 设置锁的持有者为null
op2=>operation: 重置state


e=>end: return free
op4=>operation: 抛出异常

st->cond1
cond1(yes)->op0->cond2
cond1(no)->op4

cond2(yes)->op3->op1->e
cond2(no)->e</textarea><textarea id="flowchart-1-options" style="display: none">{"scale":1,"line-width":2,"line-length":50,"text-margin":10,"font-size":12}</textarea><script>  var code = document.getElementById("flowchart-1-code").value;  var options = JSON.parse(decodeURIComponent(document.getElementById("flowchart-1-options").value));  var diagram = flowchart.parse(code);  diagram.drawSVG("flowchart-1", options);</script>{% endraw %}{% raw %}<textarea id="flowchart-2-code" style="display: none">st=>start: Start
op1=>operation: 获取当前线程
op2=>operation: 获取state

cond1=>condition: state==0 ?

cond2=>condition: 是否是头节点&&CAS获锁成功

cond3=>condition: 当前线程是否已经持有锁了



op3=>operation: 重置state


e1=>end: return true
e2=>end: return false
e3=>end: return false
st->op1->op2->cond1
cond1(yes)->cond2
cond1(no)->cond3
cond2(yes)->e1
cond2(no)->e2
cond3(yes)->op3->e1
cond3(no)->e3</textarea><textarea id="flowchart-2-options" style="display: none">{"scale":1,"line-width":2,"line-length":50,"text-margin":10,"font-size":12}</textarea><script>  var code = document.getElementById("flowchart-2-code").value;  var options = JSON.parse(decodeURIComponent(document.getElementById("flowchart-2-options").value));  var diagram = flowchart.parse(code);  diagram.drawSVG("flowchart-2", options);</script>{% endraw %}</code></pre>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/05/29/JAVA%E5%A4%9A%E7%BA%BF%E7%A8%8B-%E5%8E%9F%E5%AD%90%E7%B1%BB/" rel="next" title="JAVA多线程-原子类">
                <i class="fa fa-chevron-left"></i> JAVA多线程-原子类
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/05/31/JAVA%E5%A4%9A%E7%BA%BF%E7%A8%8B-AQS/" rel="prev" title="JAVA多线程-AQS">
                JAVA多线程-AQS <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/xiaoawo.jpg"
                alt="小蜗" />
            
              <p class="site-author-name" itemprop="name">小蜗</p>
              <p class="site-description motion-element" itemprop="description">凡心所向，素履所往，生如逆旅，一苇以航</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%7C%7C%20archive">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#ReentrantLock"><span class="nav-number">1.</span> <span class="nav-text">ReentrantLock</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、继承关系"><span class="nav-number">1.1.</span> <span class="nav-text">一、继承关系</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、成员变量"><span class="nav-number">1.2.</span> <span class="nav-text">二、成员变量</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、Sync的具体实现"><span class="nav-number">1.2.1.</span> <span class="nav-text">1、Sync的具体实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、NonfairSync的具体实现"><span class="nav-number">1.2.2.</span> <span class="nav-text">2、NonfairSync的具体实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、FairSync的具体实现"><span class="nav-number">1.2.3.</span> <span class="nav-text">3、FairSync的具体实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4、小结"><span class="nav-number">1.2.4.</span> <span class="nav-text">4、小结</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三、构造方法"><span class="nav-number">1.3.</span> <span class="nav-text">三、构造方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四、主要方法"><span class="nav-number">1.4.</span> <span class="nav-text">四、主要方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、lock"><span class="nav-number">1.4.1.</span> <span class="nav-text">1、lock</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、unlock"><span class="nav-number">1.4.2.</span> <span class="nav-text">2、unlock</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小蜗</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
